using System;
using System.Drawing;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ctsu.Admin.BusLogic;

namespace ctsu.Admin.Forms
{	
	public class ReportCostCenterCriteriaDates : System.Windows.Forms.Form
	{
		private DataTable _dtReport;
		private DataView _dvReport;
		private bool _isGridInitialized = false;

		#region windows controls
		private System.Windows.Forms.GroupBox gbxCriteria;
		private System.Windows.Forms.TextBox Count;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button btnRun;
		private System.Windows.Forms.Label label2;
		private Syncfusion.Windows.Forms.Tools.DateTimePickerAdv BeginDate;
		private Syncfusion.Windows.Forms.Tools.DateTimePickerAdv EndDate;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.DataGrid dg;
		private System.Windows.Forms.Button btnExit;
		private System.Windows.Forms.Button btnPrint;
		private System.ComponentModel.Container components = null;
		#endregion windows controls

		public ReportCostCenterCriteriaDates()
		{
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.gbxCriteria = new System.Windows.Forms.GroupBox();
            this.EndDate = new Syncfusion.Windows.Forms.Tools.DateTimePickerAdv();
            this.label1 = new System.Windows.Forms.Label();
            this.Count = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.btnRun = new System.Windows.Forms.Button();
            this.BeginDate = new Syncfusion.Windows.Forms.Tools.DateTimePickerAdv();
            this.label2 = new System.Windows.Forms.Label();
            this.dg = new System.Windows.Forms.DataGrid();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnPrint = new System.Windows.Forms.Button();
            this.gbxCriteria.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.EndDate)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.EndDate.Calendar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.BeginDate)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.BeginDate.Calendar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
            this.SuspendLayout();
            // 
            // gbxCriteria
            // 
            this.gbxCriteria.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.gbxCriteria.Controls.Add(this.EndDate);
            this.gbxCriteria.Controls.Add(this.label1);
            this.gbxCriteria.Controls.Add(this.Count);
            this.gbxCriteria.Controls.Add(this.label3);
            this.gbxCriteria.Controls.Add(this.btnRun);
            this.gbxCriteria.Controls.Add(this.BeginDate);
            this.gbxCriteria.Controls.Add(this.label2);
            this.gbxCriteria.Location = new System.Drawing.Point(8, 0);
            this.gbxCriteria.Name = "gbxCriteria";
            this.gbxCriteria.Size = new System.Drawing.Size(504, 56);
            this.gbxCriteria.TabIndex = 1;
            this.gbxCriteria.TabStop = false;
            this.gbxCriteria.Text = "Criteria";
            this.gbxCriteria.Enter += new System.EventHandler(this.gbxCriteria_Enter);
            // 
            // EndDate
            // 
            this.EndDate.BackgroundColor = new Syncfusion.Drawing.BrushInfo();
            this.EndDate.Border3DStyle = System.Windows.Forms.Border3DStyle.Flat;
            this.EndDate.BorderColor = System.Drawing.SystemColors.ControlDark;
            this.EndDate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            // 
            // 
            // 
            this.EndDate.Calendar.AllowMultipleSelection = false;
            this.EndDate.Calendar.Culture = new System.Globalization.CultureInfo("");
            this.EndDate.Calendar.DaysFont = new System.Drawing.Font("Verdana", 8F);
            this.EndDate.Calendar.DaysHeaderInterior = new Syncfusion.Drawing.BrushInfo(System.Drawing.SystemColors.Window);
            this.EndDate.Calendar.Dock = System.Windows.Forms.DockStyle.Fill;
            this.EndDate.Calendar.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.EndDate.Calendar.GridLines = Syncfusion.Windows.Forms.Grid.GridBorderStyle.None;
            this.EndDate.Calendar.HeaderHeight = 20;
            this.EndDate.Calendar.HeaderStartColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(128)))), ((int)(((byte)(62)))));
            this.EndDate.Calendar.Location = new System.Drawing.Point(0, 0);
            this.EndDate.Calendar.MinValue = new System.DateTime(1753, 1, 1, 0, 0, 0, 0);
            this.EndDate.Calendar.Name = "monthCalendar";
            // 
            // 
            // 
            this.EndDate.Calendar.NoneButton.Location = new System.Drawing.Point(131, 0);
            this.EndDate.Calendar.ScrollButtonSize = new System.Drawing.Size(24, 24);
            this.EndDate.Calendar.SelectedDates = new System.DateTime[] {
        new System.DateTime(2004, 12, 17, 9, 3, 47, 453)};
            this.EndDate.Calendar.Size = new System.Drawing.Size(203, 174);
            this.EndDate.Calendar.SizeToFit = true;
            this.EndDate.Calendar.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
            this.EndDate.Calendar.TabIndex = 0;
            // 
            // 
            // 
            this.EndDate.Calendar.TodayButton.Location = new System.Drawing.Point(0, 0);
            this.EndDate.Calendar.TodayButton.Size = new System.Drawing.Size(131, 20);
            this.EndDate.Calendar.WeekFont = new System.Drawing.Font("Verdana", 8F);
            this.EndDate.Calendar.WeekInterior = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Vertical, System.Drawing.Color.PeachPuff, System.Drawing.Color.AntiqueWhite);
            this.EndDate.DropDownImage = null;
            this.EndDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
            this.EndDate.Location = new System.Drawing.Point(312, 24);
            this.EndDate.Name = "EndDate";
            this.EndDate.NullString = "";
            this.EndDate.ShowCheckBox = false;
            this.EndDate.Size = new System.Drawing.Size(88, 20);
            this.EndDate.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
            this.EndDate.TabIndex = 8;
            this.EndDate.Value = new System.DateTime(2004, 12, 17, 9, 3, 47, 453);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(256, 24);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 16);
            this.label1.TabIndex = 7;
            this.label1.Text = "End date:";
            // 
            // Count
            // 
            this.Count.Location = new System.Drawing.Point(456, 24);
            this.Count.Name = "Count";
            this.Count.ReadOnly = true;
            this.Count.Size = new System.Drawing.Size(40, 20);
            this.Count.TabIndex = 6;
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(416, 24);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(40, 16);
            this.label3.TabIndex = 5;
            this.label3.Text = "Count:";
            // 
            // btnRun
            // 
            this.btnRun.Location = new System.Drawing.Point(8, 24);
            this.btnRun.Name = "btnRun";
            this.btnRun.Size = new System.Drawing.Size(75, 23);
            this.btnRun.TabIndex = 4;
            this.btnRun.Text = "Run";
            this.btnRun.Click += new System.EventHandler(this.btnRun_Click);
            // 
            // BeginDate
            // 
            this.BeginDate.BackgroundColor = new Syncfusion.Drawing.BrushInfo();
            this.BeginDate.Border3DStyle = System.Windows.Forms.Border3DStyle.Flat;
            this.BeginDate.BorderColor = System.Drawing.SystemColors.ControlDark;
            this.BeginDate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            // 
            // 
            // 
            this.BeginDate.Calendar.AllowMultipleSelection = false;
            this.BeginDate.Calendar.Culture = new System.Globalization.CultureInfo("");
            this.BeginDate.Calendar.DaysFont = new System.Drawing.Font("Verdana", 8F);
            this.BeginDate.Calendar.DaysHeaderInterior = new Syncfusion.Drawing.BrushInfo(System.Drawing.SystemColors.Window);
            this.BeginDate.Calendar.Dock = System.Windows.Forms.DockStyle.Fill;
            this.BeginDate.Calendar.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.BeginDate.Calendar.GridLines = Syncfusion.Windows.Forms.Grid.GridBorderStyle.None;
            this.BeginDate.Calendar.HeaderHeight = 20;
            this.BeginDate.Calendar.HeaderStartColor = System.Drawing.Color.FromArgb(((int)(((byte)(254)))), ((int)(((byte)(128)))), ((int)(((byte)(62)))));
            this.BeginDate.Calendar.Location = new System.Drawing.Point(0, 0);
            this.BeginDate.Calendar.MinValue = new System.DateTime(1753, 1, 1, 0, 0, 0, 0);
            this.BeginDate.Calendar.Name = "monthCalendar";
            // 
            // 
            // 
            this.BeginDate.Calendar.NoneButton.Location = new System.Drawing.Point(131, 0);
            this.BeginDate.Calendar.ScrollButtonSize = new System.Drawing.Size(24, 24);
            this.BeginDate.Calendar.SelectedDates = new System.DateTime[] {
        new System.DateTime(2004, 12, 17, 9, 3, 47, 453)};
            this.BeginDate.Calendar.Size = new System.Drawing.Size(203, 174);
            this.BeginDate.Calendar.SizeToFit = true;
            this.BeginDate.Calendar.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
            this.BeginDate.Calendar.TabIndex = 0;
            // 
            // 
            // 
            this.BeginDate.Calendar.TodayButton.Location = new System.Drawing.Point(0, 0);
            this.BeginDate.Calendar.TodayButton.Size = new System.Drawing.Size(131, 20);
            this.BeginDate.Calendar.WeekFont = new System.Drawing.Font("Verdana", 8F);
            this.BeginDate.Calendar.WeekInterior = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.Vertical, System.Drawing.Color.PeachPuff, System.Drawing.Color.AntiqueWhite);
            this.BeginDate.DropDownImage = null;
            this.BeginDate.Format = System.Windows.Forms.DateTimePickerFormat.Short;
            this.BeginDate.Location = new System.Drawing.Point(160, 24);
            this.BeginDate.Name = "BeginDate";
            this.BeginDate.NullString = "";
            this.BeginDate.ShowCheckBox = false;
            this.BeginDate.Size = new System.Drawing.Size(88, 20);
            this.BeginDate.Style = Syncfusion.Windows.Forms.VisualStyle.Office2003;
            this.BeginDate.TabIndex = 3;
            this.BeginDate.Value = new System.DateTime(2004, 12, 17, 9, 3, 47, 453);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(96, 24);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(64, 16);
            this.label2.TabIndex = 2;
            this.label2.Text = "Begin date:";
            // 
            // dg
            // 
            this.dg.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dg.DataMember = "";
            this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dg.Location = new System.Drawing.Point(0, 64);
            this.dg.Name = "dg";
            this.dg.Size = new System.Drawing.Size(520, 360);
            this.dg.TabIndex = 2;
            // 
            // btnExit
            // 
            this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnExit.Location = new System.Drawing.Point(272, 432);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(75, 23);
            this.btnExit.TabIndex = 5;
            this.btnExit.Text = "Exit";
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            // 
            // btnPrint
            // 
            this.btnPrint.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.btnPrint.Enabled = false;
            this.btnPrint.Location = new System.Drawing.Point(184, 432);
            this.btnPrint.Name = "btnPrint";
            this.btnPrint.Size = new System.Drawing.Size(75, 23);
            this.btnPrint.TabIndex = 4;
            this.btnPrint.Text = "Print";
            this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
            // 
            // ReportCostCenterCriteriaDates
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(520, 462);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnPrint);
            this.Controls.Add(this.dg);
            this.Controls.Add(this.gbxCriteria);
            this.Name = "ReportCostCenterCriteriaDates";
            this.Text = "Cost Center Report - Criteria Dates";
            this.gbxCriteria.ResumeLayout(false);
            this.gbxCriteria.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.EndDate.Calendar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.EndDate)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.BeginDate.Calendar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.BeginDate)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private void InitializeGrid()
		{
			//size grid columns	
			this._dvReport = new DataView(this._dtReport);
			dg.DataSource = this._dvReport;
			DataGridTableStyle dgts = new DataGridTableStyle();  
			dgts.MappingName = "Report";			
			dg.TableStyles.Add(dgts);
			
			for(int i=0;i<this._dtReport.Columns.Count;i++)
			{
				dgts.GridColumnStyles[i].Width = (int)_dtReport.Columns[i].ExtendedProperties["Width"]; 
				dgts.GridColumnStyles[i].HeaderText = _dtReport.Columns[i].Caption;			
			}
			_isGridInitialized = true;
		}

		private bool VerifyDates(out string message)
		{
			message = "";
			if(BeginDate.IsNullDate || EndDate.IsNullDate)
			{
				message = "Enter both a Begin Date and an End Date";
				return false;
			}

			if(BeginDate.Value.CompareTo(EndDate.Value) > -1)
			{
				message = "End Date must be greater than Begin Date";
				return false;
			}
			return true;
		}

		private void btnRun_Click(object sender, System.EventArgs e)
		{
			string message = "";
			if(! VerifyDates(out message))
			{
				MessageBox.Show(this, message, "Invalid Date", MessageBoxButtons.OK, MessageBoxIcon.Stop);
				return;
			}

			dg.DataSource = null;
			btnPrint.Enabled = false;
			this._dtReport = ArpUtils.GetCostCentersChargedForDates(BeginDate.Value,EndDate.Value);
			if(_dtReport == null)
				MessageBox.Show(this, "No records found for this criteria." , "No Records Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
			
			if(!_isGridInitialized)
				InitializeGrid();
			this._dvReport = new DataView(this._dtReport);
			dg.DataSource = this._dvReport;
			Count.Text = this._dtReport.Rows.Count.ToString();
			if(this._dtReport.Rows.Count>0)
				btnPrint.Enabled = true;
		}

		private void btnPrint_Click(object sender, System.EventArgs e)
		{						
			string criteria = "Criteria: Begin date: " + BeginDate.Value.ToShortDateString() + 
				" - End date: " + EndDate.Value.ToShortDateString() +
                "     Count: " + Count.Text;
			string[] title = new string[3];
			title[0] = "GCRC - Cost Center Report";
			title[1] = "( Cost Centers Charged During Criteria Dates )";
			title[2] = criteria;
			DataView dv = (DataView)dg.DataSource;
			PrintingUtils pu = new PrintingUtils(dv, title, this, false);
			pu.Initialize();			
		}

		private void btnExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void gbxCriteria_Enter(object sender, System.EventArgs e)
		{
		
		}
	}
}
